<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class SendPhoneMsg implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    var $info;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($phone,$temp_id,$data)
    {
        $this->info = [
            "phone"          => $phone,
            "temp_id"        => config("svs.aliyun_dysms.$temp_id"),
            "data"           => $data,
        ];
        logger("get_phone_msg_job:",$this->info);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        logger(" send_phone_msg  handle ... ");

        //
        $info    = $this->info;
        $phone   = array_get($info,"phone");
        $temp_id = array_get($info,"temp_id");
        $data    = array_get($info,"data");

       

        // if( app()->environment('release') ) {
            //正式环境
            $res = $this->sendSms($phone,$temp_id,$data);
            logger("send_phone_res:" . json_encode($res));
        // }
        logger("send_phone_msg:",$data);
    }

    private function sendSms($phone, $template_code, $data, $sign_name="SVS"){
        $params = array ();

        // *** 需用户填写部分 ***

        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = "LTAIrUYUW0bbudo7";
        $accessKeySecret = "gk7noYRtWwCsnNemgqZgiOrgCVbWZn";

        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $phone;

        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = $sign_name;

        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = $template_code;

        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = $data;

        // fixme 可选: 设置发送短信流水号
        //$params['OutId'] = "12345";

        // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
        //$params['SmsUpExtendCode'] = "1234567";


        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new \App\Libs\Aliyun\SignatureHelper();

        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            ))
            // fixme 选填: 启用https
            // ,true
        );

        return $content;
    }
}
